#include "main.h"

uint8_t WiperCondata; 

/**
  * @brief  串口1初始化
  * @param  baud：波特率
  * @retval None
  */
uint32_t USART1_Init(uint32_t baud)   
{
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_USART1,ENABLE);  //开启串口1与GPIOA时钟

	GPIO_InitTypeDef GPIO_InitStruct;	
	GPIO_InitStruct.GPIO_Pin = USART1_TX_PIN;   	   //串口发送引脚TX  GPIO_Pin_9
	GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF_PP; 	   //复用推挽输出
	GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;     
	GPIO_Init(GPIOA,&GPIO_InitStruct);
	
	GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IPU;          //上拉输入
	GPIO_InitStruct.GPIO_Pin = USART1_RX_PIN;		   //串口接收引脚RX GPIO_Pin_10
	GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;	   //可没有	
	GPIO_Init(GPIOA,&GPIO_InitStruct);	
	
	
	USART_InitTypeDef USART_InitStruct;
	
	USART_InitStruct.USART_BaudRate = baud;   								 	 //串口波特率
	USART_InitStruct.USART_Mode = USART_Mode_Tx | USART_Mode_Rx;				 //收发模式
	USART_InitStruct.USART_Parity = USART_Parity_No;    						 //不使用奇偶校验
	USART_InitStruct.USART_StopBits = USART_StopBits_1;                          //停止位:1位
	USART_InitStruct.USART_WordLength = USART_WordLength_8b;                     //8位数据长度
	USART_InitStruct.USART_HardwareFlowControl = USART_HardwareFlowControl_None; //不使用硬件控制流		
	USART_Init(USART1,&USART_InitStruct); 									     //初始化串口配置
	USART_LINBreakDetectLengthConfig(USART1,USART_LINBreakDetectLength_11b);	 //设置USART1_LIN断开符检测长度：11位
	

	USART_Cmd(USART1,ENABLE);	  //使能串口时钟
	USART_LINCmd(USART1,ENABLE);  //使能串口LIN模式
	
	NVIC_InitTypeDef  NVIC_InitStruct;        		//定义NVIC结构体	
	NVIC_InitStruct.NVIC_IRQChannel = USART1_IRQn;
	NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
	NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 0;//配置抢占优先级为最高优先级
	NVIC_InitStruct.NVIC_IRQChannelSubPriority = 0;//配置响应优先级为最高优先级	
	NVIC_Init(&NVIC_InitStruct);

	USART_ITConfig(USART1,USART_IT_RXNE,ENABLE);		//使能串口接收中断
	USART_ITConfig(USART1, USART_IT_LBD,ENABLE);        //打开LIN断开符检测中断	
	//USART_ITConfig(USART1, USART_IT_IDLE,ENABLE);       //开启空闲中断
	//LIN_BUF_Init(&LINRxBuffer,(LIN_MSG*)LINRxDataBuf,LIN_BUFFER_SIZE);
	return baud; //返回波特率
}


void USART2_Init(uint32_t baud)   
{
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE);  //开启串口2时钟
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA ,ENABLE);  //开启GPIOA时钟

	GPIO_InitTypeDef GPIO_InitStruct;	
	GPIO_InitStruct.GPIO_Pin = USART2_TX_PIN;   	   //串口发送引脚TX  GPIO_Pin_9
	GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF_PP; 	   //复用推挽输出
	GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;     
	GPIO_Init(GPIOA,&GPIO_InitStruct);
	
	GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IPU;          //上拉输入
	GPIO_InitStruct.GPIO_Pin = USART2_RX_PIN;		   //串口接收引脚RX GPIO_Pin_10
	GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;	   //可没有	
	GPIO_Init(GPIOA,&GPIO_InitStruct);	
	
	USART_InitTypeDef USART_InitStruct;
	
	USART_InitStruct.USART_BaudRate = baud;   								 	 //串口波特率
	USART_InitStruct.USART_Mode = USART_Mode_Tx | USART_Mode_Rx;				 //收发模式
	USART_InitStruct.USART_Parity = USART_Parity_No;    						 //不使用奇偶校验
	USART_InitStruct.USART_StopBits = USART_StopBits_1;                          //停止位:1位
	USART_InitStruct.USART_WordLength = USART_WordLength_8b;                     //8位数据长度
	USART_InitStruct.USART_HardwareFlowControl = USART_HardwareFlowControl_None; //不使用硬件控制流		
	USART_Init(USART2,&USART_InitStruct); 									     //初始化串口配置
	

	USART_Cmd(USART2,ENABLE);	  //使能串口时钟
	
	NVIC_InitTypeDef  NVIC_InitStruct;        		//定义NVIC结构体	
	NVIC_InitStruct.NVIC_IRQChannel = USART2_IRQn;
	NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
	NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 1;//配置抢占优先级为最高优先级
	NVIC_InitStruct.NVIC_IRQChannelSubPriority = 0;//配置响应优先级为最高优先级	
	NVIC_Init(&NVIC_InitStruct);

	USART_ITConfig(USART2,USART_IT_RXNE,ENABLE);		//使能串口接收中断
}



void USART2_IRQHandler(void)
{
	if (USART_GetITStatus(USART2, USART_IT_RXNE) == SET)
	{
		WiperCondata = USART_ReceiveData(USART2);
		USART_ClearITPendingBit(USART2, USART_IT_RXNE);
	}
}




int fputc(int ch, FILE *f)/*重定向c库函数printf到串口,重定向后可使用printf函数*/
{
	USART_SendData(USART1,(uint8_t) ch);
	while(USART_GetFlagStatus(USART1,USART_FLAG_TXE)==RESET);
	
	return (ch);	
}  /*printf("\r\n");*/

//int fgetc(FILE *f)      /*重定向C库函数scanf到串口,重写后可使用scanf,gerchar等函数*/
//{
//	while(USART_GetFlagStatus(USART1,USART_FLAG_RXNE)==RESET);
//	
//	return (int)USART_ReceiveData(USART1);
//}









	

   
